package org.mattpayne.jrac.people.daoimpl.xml;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.mattpayne.jrac.people.dao.PeopleDao;
import org.mattpayne.jrac.people.dao.PeopleDaoExeception;

import bus.Person;

public class XmlDao implements PeopleDao {

	private String fileName;

	public XmlDao() {
		this.fileName = null;
	}

	// Create...
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mattpayne.jrac.people.dao.PeopleDao#addPerson(bus.Person)
	 */
	public void addPerson(Person person) throws PeopleDaoExeception {
		try {
			List lst = getPeople();
			if (null == lst) {
				lst = new ArrayList();
			}
			lst.add(person);
			saveAll(lst);
		} catch (Exception bland) {
			bland.printStackTrace();
			throw new PeopleDaoExeception("Could not save " + person
					+ " to file " + fileName + " problem exception=" + bland);
		}
	}

	private void saveAll(List lst) throws FileNotFoundException {
		XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
				new FileOutputStream(fileName)));
		encoder.writeObject(lst);
		encoder.close();
	}

	// Read...
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mattpayne.jrac.people.dao.PeopleDao#getPeople()
	 */
	public List getPeople() {
		List lstPeople = null;

		XMLDecoder decoder;
		try {
			decoder = new XMLDecoder(new FileInputStream(fileName));
			lstPeople = (List) decoder.readObject();
			decoder.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}

		return lstPeople;
	}

	// Update...
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mattpayne.jrac.people.dao.PeopleDao#savePerson(bus.Person)
	 */
	public void savePerson(Person person) throws PeopleDaoExeception {
		List lstPeople = getPeople();
		lstPeople = null != lstPeople ? lstPeople : new ArrayList();
		Person oldPerson = null;
		boolean bFound = false;
			// the email is considered the key....
			for (Iterator it = lstPeople.iterator(); it.hasNext();) {
				oldPerson = (Person) it.next();
				String email = oldPerson.getEmail();
				email = "" + email; // TODO: If we keep using the XmlDao find a better way.
				// My guess is we will soon be onto a better MySqlDao!
				if (email.equalsIgnoreCase(person.getEmail())) {
					bFound=true;
					break;
				}
			}
		
		if (bFound) {
			lstPeople.remove(oldPerson);
		}
		lstPeople.add(person);
		try {
			saveAll(lstPeople);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	// Delete...
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mattpayne.jrac.people.dao.PeopleDao#deletePerson(bus.Person)
	 */
	public void deletePerson(Person person) throws PeopleDaoExeception {

		List lst = getPeople();
		lst.remove(person);
		try {
			saveAll(lst);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new PeopleDaoExeception("Could not delete " + person
					+ " from file " + fileName + " problem exception=" + e);
		}
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
		p("**********\n**********\n***********\nJust set fileName=" + fileName);
	}

	public String toString() {
		return this.getClass().getName() + " with backing fileName=" + fileName;
	}

	private void p(String msg) {
		System.out.println("XmlDao "+msg);
	}

}
